package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.github.pagehelper.PageHelper;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**跨域注解
 * @CrossOrigin
 */
@Controller
@CrossOrigin
@RequestMapping("/wap")
public class ProductClassfyApiController {
    @Autowired
    ClassifyService classifyService;

    @Autowired
    ProductApisService productApisService;

    @Autowired
    ProductService productService;

    @Autowired
    ProductGuiGeService productGuiGeService;

    @Autowired
    ProductPriceService productPriceService;

    @Autowired
    IndexProductService indexProductService;
    /**
     * 商品分类
     * @return
     */
    @RequestMapping("/productClassfy")
    public ModelAndView productClassfy(){

        ModelAndView modelAndView=new ModelAndView("shoppingApi/product/classify");
        Map<String ,Object> map=new HashMap<>();
        List<Classify> RJClassifyList=null;
        List<Classify> SJClassifyList=null;

        //查询以及分类
        List<Classify> classifyList=new ArrayList<>();
        classifyList= classifyService.selectYJClassify();

        for (int i = 0; i <classifyList.size(); i++) {
            int classId=classifyList.get(i).getClassId();

            //二级分类
            RJClassifyList=classifyService.selectRJClassify(classId);

        }
        map.put("SJClassify",SJClassifyList);
        map.put("RJClassifyList",RJClassifyList);

        map.put("classfyList",classifyList);
        modelAndView.addObject("map",map);

        return modelAndView;
    }



    @ResponseBody
    @RequestMapping("/selectProductYJAndRjClassfy")
    public ModelAndView selectProductYJAndRjClassfy(){
        ModelAndView modelAndView=new ModelAndView("shoppingApi/product/classify");
        Map<String,Object> map=new HashMap<>();
        List<Classify> allRJClassifyList=new ArrayList<>();

        //一级分类
        List<Classify> yjclassifyList= classifyService.selectYJClassify();
        map.put("yjclassifyList",yjclassifyList);

        for (int i = 0; i <yjclassifyList.size(); i++) {
            //根据以及id查询二级所有分类
            //二级分类

            int classId=yjclassifyList.get(i).getClassId();
            List<Classify>   rjClassfyList=classifyService.selectRJClassify(classId);

            map.put("rjClassfyList",rjClassfyList);

        }

        modelAndView.addObject("map",map);
        return  modelAndView;
    }

    /*根据一级分类ID查询二级分类*/
    @ResponseBody
    @RequestMapping("getAllRJClassifyByYJClassId")
    public Map<String ,Object>getAllRJClassifyByYJClassId(int classId){
        Map<String,Object> map=new HashMap<>();
        List<Classify>   rjClassfyList=classifyService.selectRJClassify(classId);
        map.put("rjClassfyList", rjClassfyList);
        return map;
    }




/*修改三级分类*/
@ResponseBody
@RequestMapping("/selectProductSjClassfy")
public ModelAndView selectProductSjClassfy(HttpServletRequest request){

    String rjClassId=request.getParameter("rjClassId");
    int rjClassIds=0;
    if(rjClassId!=null&&rjClassId!=""){
        rjClassIds=Integer.parseInt(rjClassId);
    }
    Map<String,Object> map=new HashMap<>();
    ModelAndView modelAndView=new ModelAndView("shoppingApi/product/productSjClassify");

            //根据二级分类id查询三级分类的商品
            Classify classify=classifyService.selectByPrimaryKey(rjClassIds);
            if(classify!=null){
                int classId=classify.getUpperRankId();
               List<Classify> RJClassifyList= classifyService.selectRJClassify(classId);
                map.put("RJClassifyList",RJClassifyList);
            }



    IndexProduct indexProduct=new IndexProduct();
    indexProduct.setProductUpState("Y");
    indexProduct.setClassId(rjClassIds);
    List<IndexProduct> indexProductList=indexProductService.selectProductListByRJClassIdAndUpState(indexProduct);
    map.put("indexProductList",indexProductList);
    modelAndView.addObject("map",map);
    return  modelAndView;
}



    /*修改三级分类*/
    @ResponseBody
    @RequestMapping("/selectSjClassfyByClassId")
    public Map<String,Object> selectSjClassfyByClassId(HttpServletRequest request){

        String rjClassId=request.getParameter("rjClassId");
        int rjClassIds=0;
        if(rjClassId!=null&&rjClassId!=""){
            rjClassIds=Integer.parseInt(rjClassId);
        }
        Map<String,Object> map=new HashMap<>();

        IndexProduct indexProduct=new IndexProduct();
        indexProduct.setProductUpState("Y");
        indexProduct.setClassId(rjClassIds);
        List<IndexProduct> indexProductList=indexProductService.selectProductListByRJClassIdAndUpState(indexProduct);
        map.put("indexProductList",indexProductList);
       return map;
    }




    /**
     * 三级分类列表
     * @return
     */
    @RequestMapping("/productSJClassify")
    public ModelAndView productSJClassify(){
        ModelAndView modelAndView=new ModelAndView("shoppingApi/product/productSjClassify");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/selectProSjClassfy")
    public Map<String,Object> selectProSjClassfy(HttpServletRequest request){

        String rjClassId=request.getParameter("rjClassId");
        String pageNum=request.getParameter("pageNum");
        List<ProductApis> productApisList=new ArrayList<>();
        List<ProductHot> productHotList=new ArrayList<>();
        ProductHot productHot=null;
        double productPrice=0.00;


        Map<String,Object> map=new HashMap<>();
        int pageNums=0;
        if(pageNum!=""&&pageNum!=null){
            pageNums=Integer.parseInt(pageNum);
        }

        if(rjClassId!=""&&rjClassId!=null){
            int rjClassIds=Integer.parseInt(rjClassId);
            //根据二级分类id查询三级分类的商品
            Classify classify=classifyService.selectByPrimaryKey(rjClassIds);
            if(classify!=null){
                int classId=classify.getUpperRankId();
                List<Classify> RJClassifyList= classifyService.selectRJClassify(classId);
                map.put("RJClassifyList",RJClassifyList);
            }

            List<Product> sjclassifyList=productService.selectProductByRJClassId(rjClassIds);
            //根据二级分类id查询三级分类的商品
            PageHelper.startPage(pageNums,5);
            List<Product> productList=productService.selectProductByRJClassId(rjClassIds);
            for (int i = 0; i <productList.size() ; i++) {
                productHot=  new ProductHot();
                int productId=productList.get(i).getProductId();
                productHot.setProId(productId);
                productHot.setProDesc(productList.get(i).getProductDesc());
                productHot.setProImg(productList.get(i).getProImg());

                //根据商品id查询规格
                List<ProductGuiGe> productGuiGeList=productGuiGeService.selectGuigeListByProductId(productId);

                if(productGuiGeList.size()>0){
                    //该商品存在多个规格
                    int guigeId=productGuiGeList.get(0).getGuigeId();
                    ProductPrice productPrice1=productPriceService.selectProductPriceByGuigeId(guigeId);
                    double proPrice=0.00;
                    if(productPrice1!=null&&productPrice1.getProPrice()!=null){
                        proPrice= productPrice1.getProPrice().doubleValue();
                    }else {
                        proPrice=0.00;
                    }
                    productHot.setProductPrice(proPrice);
                }else{
                    //该商品不存在规格
                    ProductPrice productPrice1=null;
                    List<ProductPrice> productPricesList=productPriceService.selectProductPriceByProId(productId);
                    if(productPricesList.size()!=0){
                        productPrice1=productPricesList.get(0);
                    }

                    double proPrice=0.00;
                    if(productPrice1!=null&&productPrice1.getProPrice()!=null){
                        proPrice= productPrice1.getProPrice().doubleValue();
                    }else {
                        proPrice=0.00;
                    }
                    productHot.setProductPrice(proPrice);
                }
                productHotList.add(i,productHot);
            }
            map.put("productHotList",productHotList);

        }

        return  map;
    }

/*点击二级分类名称查询该类名下的所有三级商品*/
@ResponseBody
@RequestMapping("/selectSjClassfy")
public Map<String,Object> selectSjClassfy(HttpServletRequest request){

    String rjClassId=request.getParameter("rjClassId");

    List<ProductApis> productApisList=new ArrayList<>();
    List<ProductHot> productHotList=new ArrayList<>();
    ProductHot productHot=null;
    double productPrice=0.00;


    Map<String,Object> map=new HashMap<>();


    if(rjClassId!=""&&rjClassId!=null){
        int rjClassIds=Integer.parseInt(rjClassId);
        //根据二级分类id查询三级分类的商品
        Classify classify=classifyService.selectByPrimaryKey(rjClassIds);
        if(classify!=null){
            int classId=classify.getUpperRankId();
            List<Classify> RJClassifyList= classifyService.selectRJClassify(classId);
            map.put("RJClassifyList",RJClassifyList);
        }

        List<Product> sjclassifyList=productService.selectProductByRJClassId(rjClassIds);
        //根据二级分类id查询三级分类的商品
        List<Product> productList=productService.selectProductByRJClassId(rjClassIds);
        for (int i = 0; i <productList.size() ; i++) {
            productHot=  new ProductHot();
            int productId=productList.get(i).getProductId();
            productHot.setProId(productId);
            productHot.setProDesc(productList.get(i).getProductDesc());
            productHot.setProImg(productList.get(i).getProImg());

            //根据商品id查询规格
            List<ProductGuiGe> productGuiGeList=productGuiGeService.selectGuigeListByProductId(productId);

            if(productGuiGeList.size()>0){
                //该商品存在多个规格
                int guigeId=productGuiGeList.get(0).getGuigeId();
                ProductPrice productPrice1=productPriceService.selectProductPriceByGuigeId(guigeId);
                double proPrice=0.00;
                if(productPrice1!=null&&productPrice1.getProPrice()!=null){
                    proPrice= productPrice1.getProPrice().doubleValue();
                }else {
                    proPrice=0.00;
                }
                productHot.setProductPrice(proPrice);
            }else{
                //该商品不存在规格
                ProductPrice productPrice1=null;
                List<ProductPrice> productPricesList=productPriceService.selectProductPriceByProId(productId);
                    if(productPricesList.size()!=0){
                        productPrice1=productPricesList.get(0);
                    }

                double proPrice=0.00;
                if(productPrice1!=null&&productPrice1.getProPrice()!=null){
                    proPrice= productPrice1.getProPrice().doubleValue();
                }else {
                    proPrice=0.00;
                }
                productHot.setProductPrice(proPrice);
            }
            productHotList.add(i,productHot);
        }
        map.put("productHotList",productHotList);
    }
    return  map;
}


}
